package com.iknight.theory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.theory.api.TeModelLibService;
import com.iknight.theory.mapper.TeModelLibMapper;
import com.iknight.theory.model.TEModelLibModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeModelLibServiceImpl implements TeModelLibService<TEModelLibModel, String> {
    @Autowired
    private TeModelLibMapper<TEModelLibModel, String> teModelLibMapper;

    @Override
    public Object getModelLibByNameAndType(String name, String type) {
        Map param = new HashMap();
        param.put("model_no", name);
        param.put("model_type", type);
        List<TEModelLibModel> result = teModelLibMapper.selectByCond(param);
        if (null != result && result instanceof List && result.size() > 0) {
            return result.get(0);
        }
        return null;
    }

    @Override
    public List<Map> getModelLibByType(String modelType) {
        return teModelLibMapper.getModelLibByType(modelType);
    }

    @Override
    public void save(TEModelLibModel record) {
        if (null != teModelLibMapper.selectOne(record.getModel_id())) {
            teModelLibMapper.update(record);
        } else {
            teModelLibMapper.insert(record);
        }

    }

    @Override
    public void delete(String pkey) {
        teModelLibMapper.delete(pkey);
    }

    @Override
    public TEModelLibModel selectOne(String pkey) {
        return teModelLibMapper.selectOne(pkey);
    }

    @Override
    public PageInfo<TEModelLibModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
        PageHelper.startPage(pageStart, pageSize, true);
        List<TEModelLibModel> result = teModelLibMapper.selectByCond(params);
        PageInfo<TEModelLibModel> page = new PageInfo<>(result);
        return page;
    }
}
